package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.JsonResult;
import com.qf.pojo.GoodsClass;
import com.qf.service.GoodsClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goodsClass")
public class GoodsClassController {

    @Autowired
    private GoodsClassService goodsClassService;

    @RequestMapping("goodsClassFindByPage")
    @ResponseBody
    public JsonResult goodsClassFindByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "2") Integer pageSize) {

        System.out.println("goodsClassFindByPage");

        // 1. 首先设置分页
        PageHelper.startPage(pageNum, pageSize);

        // 2. 设置分页后的第一次查询有效
        List<GoodsClass> goodsClassList = goodsClassService.findAllGoodsClass();

        System.out.println(goodsClassList);

        // 3. 封装对象到PageInfo
        PageInfo<GoodsClass> goodsClassPageInfo = new PageInfo<>(goodsClassList);

        JsonResult jsonResult = JsonResult.ok();

        jsonResult.setCount(goodsClassPageInfo.getTotal());
        jsonResult.setData(goodsClassPageInfo.getList());

        return jsonResult;

    }

    @RequestMapping("goodsClassFindAll")
    @ResponseBody
    public List<GoodsClass> goodsClassFindAll() {
        return goodsClassService.findAllGoodsClass();
    }

    @RequestMapping("deleteAllGoodsClass")
    @ResponseBody
    public String deleteAllGoodsClass(Integer[] classIds) {

        System.out.println("deleteAllGoodsClass" + classIds);

        for (Integer classId : classIds) {
            System.out.println(classId);

        }

        System.out.println("------------------");

        goodsClassService.deleteAllGoodsClass(classIds);

        for (Integer classId : classIds) {
            System.out.println(classId);

        }

        return "success";

    }

    @RequestMapping("deleteGoodsClass")
    @ResponseBody
    public String deleteGoodsClass(Integer classId) {

        System.out.println("deleteGoodsClass:-----" + classId);

        Integer integer = goodsClassService.deleteGoodsClass(classId);

        return integer == 1 ? "success" : "fail";
    }

    @RequestMapping("addGoodsClass")
    public String addGoodsClass(GoodsClass goodsClass) {
        System.out.println("addGoodsClass");

        goodsClassService.addGoodsClass(goodsClass);

        return "redirect:http://localhost:8080/index.html";
    }

    @RequestMapping("updateGoodsClass")
    @ResponseBody
    public String updateGoodsClass(GoodsClass goodsClass) {
        System.out.println("updateGoodsClass:---" + goodsClass);

        goodsClassService.updateGoodsClass(goodsClass);

        return "success";
    }

    @RequestMapping("excelGoodsClass")
    public String excelGoodsClass(Integer[] classIds) {

        System.out.println("excelGoodsClass--------");

        List<GoodsClass> goodsClassList = goodsClassService.excelGoodsClass(classIds);

        return goodsClassList == null ? "fail":"success";
    }



}
